import * as UserApi from '@/api/member/user.js';
import {
	LOGIN_STATUS,
	REFRESH_STATUS,
	UID,
	OPENID,
	PLATFORM,
	USER_INFO,
	USER_MENU,
	APP_CONFIG,
	PATIENT_IFNO,
	PATIENT_LIST
} from '../../config/cache';
import Cache from '../../utils/cache';
import { encrypt, decrypt } from '../../utils/encrypt';


const state = {
	token: Cache.get(LOGIN_STATUS) || '',
	refreshToken: Cache.get(REFRESH_STATUS) || '',
	backgroundColor: "#fff",
	userInfo: Cache.get(USER_INFO) ? Cache.getEncrypt(USER_INFO) : null,
	userMenu: Cache.get(USER_MENU) ? JSON.parse(Cache.get(USER_MENU)) : null,
	uid: Cache.get(UID) || null,
	openid: Cache.get(OPENID) || null,
	homeActive: false,
	chatUrl: Cache.get('chatUrl') || '',
	systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : '',
	productType: Cache.get('productType') || '',
	appConfig: Cache.get(APP_CONFIG) ? JSON.parse(Cache.get(APP_CONFIG)) : null,
	patientInfo: Cache.get(PATIENT_IFNO) ? Cache.getEncrypt(PATIENT_IFNO) : null,
	patientList: Cache.get(PATIENT_LIST) ? Cache.getEncrypt(PATIENT_LIST) : null
};

const mutations = {
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token);
	},
	REFRESH_LOGIN(state, opt) {
		state.refreshToken = opt.refreshToken;
		Cache.set(REFRESH_STATUS, opt.refreshToken);
	},
	SETUID(state, val) {
		state.uid = val;
		Cache.set(UID, val);
	},
	OPENID(state, val) {
		state.openid = val;
		Cache.set(OPENID, val);
	},
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
		Cache.clear(USER_INFO);
	},
	BACKGROUND_COLOR(state, color) {
		state.color = color;
		document.body.style.backgroundColor = color;
	},
	UPDATE_USERINFO(state, userInfo) {
		// console.info("app UPDATE_USERINFO");
		// console.table(userInfo);
		state.userInfo = userInfo;
		const d = encrypt(userInfo);
		Cache.set(USER_INFO, d);
	},
	UPDATE_USERMENU(state, userMenu) {
		state.userMenu = userMenu;
		Cache.set(USER_MENU, userMenu);
	},
	UPDATE_APPCONFIG(state, appConfig) {
		console.info("app UPDATE_APPCONFIG");
		// console.table(appConfig);
		state.appConfig = appConfig;
		Cache.set(APP_CONFIG, appConfig);
	},
	UPDATE_PATIENTINFO(state, patient) {
		console.info("app UPDATE_PATIENTINFO");
		// console.table(patient);
		state.patientInfo = patient;
		const d = encrypt(patient);
		Cache.set(PATIENT_IFNO, d);
	},
	UPDATE_PATIENTLIST(state, patients) {
		console.info("app UPDATE_PATIENTLIST");
		// console.table(patient);
		state.patientList = patients;
		const d = encrypt(JSON.stringify(patients));
		Cache.set(PATIENT_LIST, d);
	},
	OPEN_HOME(state) {
		state.homeActive = true;
	},
	CLOSE_HOME(state) {
		state.homeActive = false;
	},
	SET_CHATURL(state, chatUrl) {
		state.chatUrl = chatUrl;
	},
	SYSTEM_PLATFORM(state, systemPlatform) {
		state.systemPlatform = systemPlatform;
		Cache.set(PLATFORM, systemPlatform);
	},
	//更新useInfo数据
	changInfo(state, payload) {
		state.userInfo[payload.amount1] = payload.amount2;
		Cache.set(USER_INFO, state.userInfo);
	},
	//商品类型，用于区分视频号商品与一般商品
	PRODUCT_TYPE(state, productType) {
		state.productType = productType;
		Cache.set('productType', productType);
	}
};

const actions = {
	USERINFO({
		state,
		commit
	}, force) {
		return new Promise(reslove => {
			console.log("getUserInfo");
			UserApi.getUserInfo().then(res => {
				console.table(res);
				commit("UPDATE_USERINFO", res.data);
				if(res.data.appMenu && res.data.appMenu.indexTop){
					commit("UPDATE_USERMENU", res.data.appMenu);
				}
				reslove(res.data);
			}).catch((res) => {
				commit("LOGOUT");
			});
		}).catch(() => {
			console.log("res");
		});
		// debugger
		// if (state.userInfo !== null && !force)
		// 	return Promise.resolve(state.userInfo);
		// else
		// 	return new Promise(reslove => {
		// 		getUserInfo().then(res => {
		// 			commit("UPDATE_USERINFO", res.data);
		// 			reslove(res.data);
		// 		});
		// 	}).catch(() => {

		// 	});
	}
};

export default {
	state,
	mutations,
	actions
};